package awesome.userinterface;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import awesome.server.WebServer;

/**
 * Console user interface for AWeSOMe server.
 * @author Chris Carpenter
 *
 */
public class AwesomeConsole {
	WebServer serverToControl;

	public AwesomeConsole(WebServer _serverToContorl) {
		this.serverToControl = _serverToContorl;
	}
	
	/**
	 * Starts the server, allows user input to control.
	 */
	public void runConsole() {
		System.out.println("Server is starting up...");
		serverToControl.startServer();
		System.out.println("Server is up.");
		
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		String input="";
		while (!input.equals("exit")) {
			System.out.print("Command: ");
			try {
				input = reader.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			switch(input.toLowerCase()){
			case "restart":
				serverToControl.restartServer();
				break;
			case "start":
				serverToControl.startServer();
				break;
			case "stop":
				serverToControl.stopServer();
				break;
			case "exit":
				break;
			case "help":
				System.out.println(
						"restart\tRestart the server\n" +
						"start\tStarts the server\n" +
						"stop\tStops the server\n" +
						"exit\tStops the server and exits\n" +
						"help\tPrints this list\n");
				break;
			default:
				System.out.println("Invalid command. Type 'help' for a list of commands.");
			}
			
		}

		serverToControl.stopServer();
	}
	
}
